package com.sx.pipeline.ui.supply.choose

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.view.SpaceItemDecoration
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ChooseServiceAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.event.ChooseDeviceEvent
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActChooseServiceBinding
import com.sx.pipeline.repos.http.req.ServiceAddDTO
import com.sx.pipeline.ui.service.ServiceVM
import io.reactivex.rxjava3.schedulers.Schedulers


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 我的服务
 */
@Route(path = RoutePath.Service.SERVICE_CHOOSE)
class ChooseServiceActivity :
    CYDataBindingBaseActivity<ActChooseServiceBinding, ServiceVM>(R.layout.act_choose_service),
    View.OnClickListener {
    private lateinit var mAdapter: ChooseServiceAdapter
    private var key = ""

    private var serviceId: Int = 0

    private var desc: String = ""

    private var serviceName: String = ""

    private var address: String = ""

    private var price:String=""

    private var images:String=""

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            RouteManager.Service.intentToServiceAdd(1, 0)
        }

        //请求设备列表
        mViewModel.getPmsServiceList(key, limit = 100)

        RxBus.getDefault().toObservable(DeviceEvent::class.java)
            .subscribeOn(Schedulers.io())
            .subscribe {
                //刷新设备列表
                mViewModel.page = 1
                mViewModel.getPmsServiceList("", limit = 100)
            }
    }

    private fun initRv(serviceList: List<ServiceAddDTO>) {
        mAdapter = ChooseServiceAdapter(this, serviceList)
        mBinding.recyclerView.layoutManager =
            GridLayoutManager(this, 2)
        mBinding.recyclerView.addItemDecoration(
            SpaceItemDecoration(10)
        )

        mBinding.recyclerView.setAdapter(mAdapter)
        if (mViewModel.page == 1) {
            if (serviceList.size != 0) {
                serviceId = serviceList[0].id!!
                serviceName = serviceList[0].title
                desc=serviceList[0].serverDescribe
                address = serviceList[0].addressDetail
                price = serviceList[0].price
                images=serviceList[0].images
                mBinding.recyclerView.visibility = View.VISIBLE
                mBinding.emptyView.visibility = View.GONE
            } else {
                mBinding.recyclerView.visibility = View.GONE
                mBinding.emptyView.visibility = View.VISIBLE
            }
            mBinding.refreshLayout.finishRefresh()
        }
        mAdapter.setSelectMode(EasyAdapter.SelectMode.SINGLE_SELECT)
        mAdapter.setSelected(0)
        //监听多选
        mAdapter.setOnItemSingleSelectListener { itemPosition, isSelected ->
            serviceId = serviceList[itemPosition].id!!
            serviceName = serviceList[itemPosition].title
            desc=serviceList[itemPosition].serverDescribe
            address = serviceList[itemPosition].addressDetail
            price = serviceList[itemPosition].price
            images = serviceList[itemPosition].images
        }

    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.deviceListEvent.observe(this) {
            it?.let { it1 -> initRv(it1) }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.getPmsServiceList(key, limit = 100)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.getPmsServiceList(key, limit = 100)
            }
        })



        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                key = mBinding.etKeyword.text.toString()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //搜索
            R.id.tv_search -> {
                mViewModel.page = 1
                mViewModel.getPmsServiceList(key, limit = 100)
            }
            //提交选中
            R.id.tv_confirm -> {
                val intent = Intent()
                intent.putExtra("serviceId", serviceId)
                intent.putExtra("serviceName", serviceName)
                intent.putExtra("desc", desc)
                intent.putExtra("address", address)
                intent.putExtra("price",price)
                intent.putExtra("images",images)
                setResult(RESULT_OK, intent)
                finish()
            }
        }
    }

}